package sk.naive.talker.util;

import sk.naive.talker.User;

import java.util.*;

/**
 * UserContextHelper is small helper class for manipulating
 * short-term properties used in message context.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.2 $ $Date: 2004/10/02 22:59:16 $
 */
public class UserContextHelper {
    public static final String CTX_INFO = "ctxInfo";
    private User user;

    public UserContextHelper(User user) {
        this.user = user;
    }

    public void set(String ctxKey, Object value) {
        user.getProperties().put(ctxKey, value);

        Set ctxInfo = (Set) user.getProperties().get(CTX_INFO);
        if (ctxInfo == null) {
            ctxInfo = new HashSet();
            user.getProperties().put(CTX_INFO, ctxInfo);
        }
        ctxInfo.add(ctxKey);
    }

    public void cleanup() {
        Set ctxInfo = (Set) user.getProperties().get(CTX_INFO);
        if (ctxInfo != null) {
            for (Object key : ctxInfo) {
                user.getProperties().remove(key);
            }
            user.getProperties().remove(CTX_INFO);
        }
    }
}
